<template>
  <page-view avatar="" :title="false" :header-disabled="true">
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="资源名称">
                <a-input placeholder="请输入" v-model="queryParam.searchWord" :disabled="mode === 'tree'"/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="状态">
                <a-select placeholder="请选择" v-model="queryParam.status" :disabled="mode === 'tree'">
                  <a-select-option value="-1">全部</a-select-option>
                  <a-select-option value="1">正常</a-select-option>
                  <a-select-option value="0">禁用</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="search">查询</a-button>
                <a-button style="margin-left: 8px" @click="reset">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <div class="table-page-operator-wrapper">
        <a-popconfirm
          title="请选择创建类型"
          okText="按钮"
          cancelText="菜单"
          @cancel="() => createMenu()"
          @confirm="() => createButton()">
          <a-icon slot="icon" type="question-circle-o" style="color: orangered" />
          <a-button type="primary" v-hasPermission="'sys:resource:save'" ghost>新增</a-button>
        </a-popconfirm>
        <a-button v-hasPermission="'sys:resource:delete'" @click="batchDelete(selectedRowKeys)" :disabled="selectedRowKeys.length === 0">删除</a-button>
        <a-dropdown v-hasPermission="'sys:resource:export'">
          <a-menu slot="overlay">
            <a-menu-item key="export-data" @click="exprotExccel">导出Excel</a-menu-item>
          </a-menu>
          <a-button>
            更多操作 <a-icon type="down" />
          </a-button>
        </a-dropdown>

        <a-switch
          checked-children="树模式"
          un-checked-children="列模式"
          default-checked
          @change="onModeChange"
          style="width: 80px;float: right;border-radius: 4px;"/>

      </div>
      <a-table
        rowKey="id"
        :columns="columns"
        :data-source="data"
        :row-selection="rowSelection"
        :pagination="false"
        :loading="loading" >
        <template slot="name" slot-scope="text, record">
          <a-icon :type="record.icon" v-if="record.icon" style="margin-right: 6px;"/> {{ text }}
        </template>
        <template slot="type" slot-scope="text">
          <a-tag v-if="text === 1" color="orange">{{ text | getLabel('sys_res_type') }}</a-tag>
          <a-tag v-else-if="text === 2" color="green">{{ text | getLabel('sys_res_type') }}</a-tag>
          <a-tag v-else>{{ text | getLabel('sys_res_type') }}</a-tag>
        </template>
        <template slot="status" slot-scope="text">
          <a-tag v-if="text === '1'" color="green">{{ text | getLabel('sys_res_state') }}</a-tag>
          <a-tag v-else>{{ text | getLabel('sys_res_state') }}</a-tag>
        </template>
        <template slot="action" slot-scope="text, record">
          <a @click="editRow(record)" v-hasPermission="'sys:resource:update'">编辑</a>
          <a-divider type="vertical" v-hasPermission="'sys:resource:update'"/>
          <a-dropdown>
            <a class="ant-dropdown-link">
              更多 <a-icon type="down" />
            </a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="view(record)">详情</a>
              </a-menu-item>
              <a-menu-item v-if="record.status === '1'" v-hasPermission="'sys:resource:disabled'">
                <a href="javascript:;" @click="updateStatus(record)">禁用</a>
              </a-menu-item>
              <a-menu-item v-if="record.status === '0'" v-hasPermission="'sys:resource:useable'">
                <a href="javascript:;" @click="updateStatus(record)">启用</a>
              </a-menu-item>
              <a-menu-item v-hasPermission="'sys:resource:delete'">
                <a href="javascript:;" @click="deleteRecord(record)">删除</a>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </template>
      </a-table>
      <resource-menu-modal ref="menuModal" @handle-success="formHandleSuccess"/>
      <resource-button-modal ref="buttonModal" @handle-success="formHandleSuccess"/>
    </a-card>
  </page-view>
</template>

<script>
import { PageView } from '@/layouts'
import ResourceMenuModal from './modules/ResourceMenuModal'
import ResourceButtonModal from './modules/ResourceButtonModal'
import store from '@/store'

export default {
  name: 'ResourceList',
  components: { PageView, ResourceMenuModal, ResourceButtonModal },
  data () {
    return {
      mode: 'tree',
      queryParam: {
        status: '-1'
      },
      loading: false,
      columns: [
        {
          title: '资源名称',
          dataIndex: 'name',
          key: 'name',
          scopedSlots: { customRender: 'name' }
        },
        {
          title: '类型',
          dataIndex: 'type',
          key: 'type',
          width: 90,
          scopedSlots: { customRender: 'type' }
        },
        {
          title: '权限标识',
          dataIndex: 'permission',
          width: '30%',
          key: 'permission'
        },
        {
          title: '状态',
          dataIndex: 'status',
          width: 100,
          scopedSlots: { customRender: 'status' }
        }, {
          title: '操作',
          width: '150px',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' }
        }
      ],
      data: [],
      selectedRowKeys: [],
      selectedRows: [],
      rowSelection: {
        onChange: (selectedRowKeys, selectedRows) => {
          this.selectedRowKeys = selectedRowKeys
          this.selectedRows = selectedRows
        },
        onSelect: (record, selected, selectedRows) => {},
        onSelectAll: (selected, selectedRows, changeRows) => {}
      }
    }
  },
  mounted () {
    this.loadData()
  },
  methods: {
    search () {
      // 分页重置
      this.loadData()
    },
    reset () {
      // 分页重置 查询参数重置
      this.queryParam = {
        status: '-1'
      }
      this.loadData()
    },
    deleteRecord (record) {
      const that = this
      this.$confirm({
        title: '警告',
        content: `真的要删除 ${record.name} 吗?`,
        okText: '删除',
        okType: 'danger',
        cancelText: '取消',
        onOk () {
          that.handleDelete([record.id], {
            success () {},
            done () {}
          })
        }
      })
    },
    batchDelete (ids) {
      const that = this
      this.$confirm({
        title: '警告',
        content: `真的要删除这 ${ids.length} 个资源吗?`,
        okText: '删除',
        okType: 'danger',
        cancelText: '取消',
        onOk () {
          that.handleDelete(ids, {
            success () {},
            done () {}
          })
        }
      })
    },
    handleDelete (ids, callback) {
      if (ids.length > 0) {
        this.$http.delete('resource/' + ids.join(',')).then(res => {
          this.$message.success('已成功删除')
          this.loadData()
          if (callback) {
            (typeof callback.done === 'function') && callback.done.call(this)
            if (typeof callback.success === 'function') { callback.success.call(this, res) }
          }
        }).catch(e => {
          if (callback) {
            (typeof callback.done === 'function') && callback.done.call(this)

            if (typeof callback.fail === 'function') { callback.fail.call(this, e) }
          }
        })
      }
    },
    exprotExccel () {},
    loadData () {
      this.loading = true
      this.refreshStore()
      const params = Object.assign({}, this.queryParam)
      if (params.status === '-1') {
        delete params.status
      }
      this.$http.get(this.mode === 'tree' ? '/resource/tree' : 'resource/list', {
        params: params
      }).then(res => {
        this.loading = false
        this.data = res.data
      }).catch(e => {
        this.loading = false
      })
    },
    createMenu () {
      this.$refs.menuModal.show()
    },
    createButton () {
      this.$refs.buttonModal.show()
    },
    view (record) {
      if (record.type === 1) {
        this.$refs.menuModal.show(record, true)
      } else {
        this.$refs.buttonModal.show(record, true)
      }
    },
    editRow (record) {
      if (record.type === 1) {
        this.$refs.menuModal.show(record)
      } else {
        this.$refs.buttonModal.show(record)
      }
    },
    updateStatus (record) {
      const status = record.status === '1' ? '0' : '1'
      this.$http.put('/resource/updateStatus', {
        id: record.id,
        status: status
      }).then(res => {
        if (res.data) {
          this.$message.success('操作成功')
          this.loadData()
        } else {
          this.$message.error(res.data || '操作失败')
        }
      })
    },
    formHandleSuccess () {
      this.loadData()
    },
    refreshStore () {
      store.dispatch('AllResource').then(() => {})
    },
    onModeChange () {
      this.mode = this.mode === 'tree' ? 'list' : 'tree'
      this.reset()
    }
  }
}
</script>

<style scoped>

</style>
